package com.china08.yxyapi.service.manage.tec.teaching.lecture.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.GetQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.service.manage.tec.teaching.lecture.ElasticQuesService;

@Service(value = "question")
public class ElasticQuesServiceImpl implements ElasticQuesService {

    private Logger LOGGER = LoggerFactory.getLogger(ElasticQuesServiceImpl.class);

    @Resource
    private ElasticsearchTemplate elasticsearchTemplate;

    @Override
    public List<ElasticYxyYxyQuesResource> findByQuesIdIn(List<String> list) {
        List<ElasticYxyYxyQuesResource> elasticQuesList = new ArrayList<ElasticYxyYxyQuesResource>();
        if (CollectionUtils.isNotEmpty(list)) {
            NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
            NativeSearchQuery query = builder.withIds(list).build();
            elasticQuesList = elasticsearchTemplate.multiGet(query, ElasticYxyYxyQuesResource.class);
        }
        return elasticQuesList;
    }

    @Override
    public ElasticYxyYxyQuesResource findByEsQuesId(String quesId) {
        GetQuery getQuery = new GetQuery();
        getQuery.setId(quesId);
        ElasticYxyYxyQuesResource elasticQues = elasticsearchTemplate.queryForObject(getQuery,
                ElasticYxyYxyQuesResource.class);
        return elasticQues;
    }

    @Override
    public ElasticYxyYxyQuesResource findByQuestionTypeAndQuesIdIn(String questionType, List<Integer> knowIdList,
            String difficultLevel, List<String> quesIdList) {

        BoolQueryBuilder boolBuilder = new BoolQueryBuilder();
        TermsQueryBuilder builder = new TermsQueryBuilder("knowFullIds", knowIdList);
        boolBuilder.must(builder);
        TermQueryBuilder builderLevel = new TermQueryBuilder("difficultyLevel", difficultLevel);
        TermQueryBuilder builderType = new TermQueryBuilder("questionType", questionType);
        boolBuilder.must(builderLevel);
        boolBuilder.must(builderType);
        TermsQueryBuilder mustEsQuesId = new TermsQueryBuilder("id", quesIdList);
        boolBuilder.mustNot(mustEsQuesId);
        NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder();
        NativeSearchQuery nativeSearchQuery = searchQueryBuilder.withQuery(boolBuilder)
                .withPageable(new PageRequest(0, 10)).build();
        List<ElasticYxyYxyQuesResource> respElasticQues = elasticsearchTemplate.queryForList(nativeSearchQuery,
                ElasticYxyYxyQuesResource.class);
        //
        LOGGER.info(boolBuilder.toString() + "================");
        // 随机拿一道题
        ElasticYxyYxyQuesResource basicQues = null;
        if (CollectionUtils.isNotEmpty(respElasticQues)) {
            int max = respElasticQues.size();
            Random random = new Random();
            int r = random.nextInt(max);
            basicQues = respElasticQues.get(r);
        }
        return basicQues;
    }

    @Override
    public List<ElasticYxyYxyQuesResource> findTop3ByQuestionTypeAndQuesIdNotIn(String questionType,
            List<Integer> knowIdList, String difficultLevel, List<String> quesIdList) {

        List<ElasticYxyYxyQuesResource> respElasticQues = new ArrayList<ElasticYxyYxyQuesResource>();

        respElasticQues = findQuestionByDifficultLevelNext(questionType, knowIdList, difficultLevel, quesIdList);

        if (respElasticQues.size() < 3) {
            if ("3".equals(difficultLevel)) {
                difficultLevel = "2";
                respElasticQues = findQuestionByDifficultLevelNext(questionType, knowIdList, difficultLevel,
                        quesIdList);
            } else if ("2".equals(difficultLevel)) {
                difficultLevel = "1";
                respElasticQues = findQuestionByDifficultLevelNext(questionType, knowIdList, difficultLevel,
                        quesIdList);
            }
        }
        // 随机拿一道题
        List<ElasticYxyYxyQuesResource> basicQues = new ArrayList<ElasticYxyYxyQuesResource>();

        List<Integer> listIndex = new ArrayList<Integer>();
        for (int i = 0; i < 100; i++) {
            ElasticYxyYxyQuesResource basicQues1 = new ElasticYxyYxyQuesResource();
            if (CollectionUtils.isNotEmpty(respElasticQues)) {
                if (listIndex.size() < 3) {
                    int max = respElasticQues.size();
                    Random random = new Random();
                    int r = random.nextInt(max);
                    if (!listIndex.contains(r)) {
                        basicQues1 = respElasticQues.get(r);
                        basicQues.add(basicQues1);
                        listIndex.add(r);
                    }
                } else {
                    break;
                }
            }
        }
        return basicQues;
    }

    @Override
    public List<ElasticYxyYxyQuesResource> findQuestionByDifficultLevelNext(String questionType,
            List<Integer> knowIdList, String difficultLevel, List<String> quesIdList) {
        BoolQueryBuilder boolBuilder = new BoolQueryBuilder();
        TermsQueryBuilder builder = new TermsQueryBuilder("knowFullIds", knowIdList);
        boolBuilder.must(builder);
        TermQueryBuilder builderLevel = new TermQueryBuilder("difficultyLevel", difficultLevel);
        TermQueryBuilder builderType = new TermQueryBuilder("questionType", questionType);
        boolBuilder.must(builderLevel);
        boolBuilder.must(builderType);
        TermsQueryBuilder mustEsQuesId = new TermsQueryBuilder("id", quesIdList);
        boolBuilder.mustNot(mustEsQuesId);

        // 去除别人的
        TermQueryBuilder viewScopeBuilder = new TermQueryBuilder("viewScope", 0);
        boolBuilder.mustNot(viewScopeBuilder);
        NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder();
        NativeSearchQuery nativeSearchQuery = searchQueryBuilder.withQuery(boolBuilder)
                .withPageable(new PageRequest(0, 100)).build();
        List<ElasticYxyYxyQuesResource> respElasticQues = elasticsearchTemplate.queryForList(nativeSearchQuery,
                ElasticYxyYxyQuesResource.class);
        return respElasticQues;
    }

}
